package com.alexsh.service.bookspresentation.impl;

import com.alexsh.common.ApplicationContextHolder;
import com.alexsh.common.CommonFormatterUtils;
import com.alexsh.dao.impl.BooksRepository;
import com.alexsh.domain.Book;
import com.alexsh.service.bookspresentation.BookManager;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author Alexander Shmygin <AShmygin@luxoft.com>
 * @version 1.0
 */
@Service("bookInXmlManager")
public class BookInXmlManager implements BookManager {
    private static final String XML_PREFIX = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>";
    private static final String XML_BOOKS_TAG = "<books>";
    private static final String XML_PAGE_INDEX_OPEN_TAG = "<pageIndex>";
    private static final String XML_PAGE_INDEX_CLOSE_TAG = "</pageIndex>";
    private static final String XML_PAGE_SIZE_OPEN_TAG = "<pageSize>";
    private static final String XML_PAGE_SIZE_CLOSE_TAG = "</pageSize>";
    private static final String XML_TOTAL_PAGES_OPEN_TAG = "<totalPages>";
    private static final String XML_TOTAL_PAGES_CLOSE_TAG = "</totalPages>";
    private static final String XML_TOTAL_ITEMS_OPEN_TAG = "<totalItems>";
    private static final String XML_TOTAL_ITEMS_CLOSE_TAG = "</totalItems>";
    private static final String XML_POSTFIX = "</books></root>";

    private BooksRepository bookRepository
        = (BooksRepository) ApplicationContextHolder.getBean("bookRepository");

    public String getContent() {
        HashMap<String, Book> books = bookRepository.getBooks();
        StringBuilder sb = new StringBuilder();
        populateXmlHeader(sb);
        sb.append(XML_TOTAL_ITEMS_OPEN_TAG).append(books.size()).append(XML_TOTAL_ITEMS_CLOSE_TAG);
        sb.append(XML_BOOKS_TAG);
        for (Book book : books.values()) {
            sb.append(populateXmlElement(book));
        }
        sb.append(XML_POSTFIX);
        return sb.toString();
    }

    public String getById(String bookId) {
        HashMap<String, Book> books = bookRepository.getBooks();
        Book book = books.get(bookId);
        StringBuilder sb = new StringBuilder();
        populateXmlHeader(sb);
        sb.append(XML_TOTAL_ITEMS_OPEN_TAG).append("1").append(XML_TOTAL_ITEMS_CLOSE_TAG);
        sb.append(XML_BOOKS_TAG);
        sb.append(populateXmlElement(book));
        sb.append(XML_POSTFIX);
        return sb.toString();
    }

    private void populateXmlHeader(StringBuilder sb) {
        sb.append(XML_PREFIX);
        sb.append(XML_PAGE_INDEX_OPEN_TAG).append("1").append(XML_PAGE_INDEX_CLOSE_TAG);
        sb.append(XML_PAGE_SIZE_OPEN_TAG).append("10").append(XML_PAGE_SIZE_CLOSE_TAG);
        sb.append(XML_TOTAL_PAGES_OPEN_TAG).append("1").append(XML_TOTAL_PAGES_CLOSE_TAG);
    }

    private String populateXmlElement(Book book) {
        return "<book id=\"" + book.getId()
            + "\" title=\"" + book.getTitle()
            + "\" author=\"" + book.getAuthors()
            + "\" pubDate=\""
            + CommonFormatterUtils.dateToStr(book.getPubDate()) + "\"/>";
    }
}
